/**
 * Form Layout by John.
 * Functional coding by John.
 */

package Forms;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Classes.ProdSup;
import Classes.teDB;
import Forms.MainMDIForm;
import teComponents.teJButton;
import teComponents.teJComboBox;
import teComponents.teJFrame;
import teComponents.teJinternalFrame;
import teComponents.teJLabel;
import teComponents.teJLabelWarning;
import teComponents.teJList;
//Code here done by Jon, Shell by Dagny unless otherwise noted
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ProductSupplierForm  extends teComponents.teJinternalFrame {
        teDB dbCon;
        teJinternalFrame ParentWindow = null;
        ProdSup toSend;
        public ProdSup getToSend() {
                return toSend;
        }
        public ProductSupplierForm()
        {
                try {
                        dbCon=new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                initGUI();
                ParentWindow = null;
        }
        public ProductSupplierForm(teJinternalFrame sender)//teJFrame MDIParent
        {
                //myMDIParent=MDIParent;
                try {
                        dbCon=new teDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@localhost:1521:orant11g", "ictoosd", "ictoosd");
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                initGUI();
                ParentWindow=sender;
        }
        
        //Lidiya Chhailo added to handle add product supplier to the 
        //PackagesForm
        
        public void  sendData(ProdSup prsp)
        {
        	PackagesForm pk = (PackagesForm)ParentWindow;
        	System.out.println("PackagesForm" +pk);	 
    		if(pk != null)
    		  pk.addProductsSup(prsp);
        }
        
        private void initGUI() {
                try {
                        {
                                this.setTitle("Maintain Product/Suppliers");
                                {
                                        jScrollPane1 = new JScrollPane();
                                        getContentPane().add(jScrollPane1);
                                        jScrollPane1.setBounds(12, 109, 219, 238);
                                        {
                                                ListModel lstAllModel = 
                                                        new DefaultComboBoxModel();
                                                lstAll = new teJList();
                                                jScrollPane1.setViewportView(lstAll);
                                                lstAll.setModel(lstAllModel);
                                                lstAll.setBounds(12, 109, 219, 238);
                                                lstAll.addListSelectionListener(new ListSelectionListener() {
                                                        
                                                        @Override
                                                        public void valueChanged(ListSelectionEvent e) {
                                                                // TODO Auto-generated method stub
                                                                siftList();
                                                        }
                                                });
                                                
                                        }
                                }
                                {
                                        jScrollPane2 = new JScrollPane();
                                        getContentPane().add(jScrollPane2);
                                        jScrollPane2.setBounds(249, 109, 248, 238);
                                        {
                                                ListModel teJList1Model = 
                                                        new DefaultComboBoxModel();
                                                lstAssigned = new teJList();
                                                jScrollPane2.setViewportView(lstAssigned);
                                                lstAssigned.setModel(teJList1Model);
                                                lstAssigned.setBounds(249, 109, 224, 238);
                                                lstAssigned.addListSelectionListener(new ListSelectionListener() {
                                                        
                                                        @Override
                                                        public void valueChanged(ListSelectionEvent e) {
                                                                // TODO Auto-generated method stub
                                                                siftList();
                                                        }
                                                });
                                        }
                                }
                        }
                        {
                                this.setSize(832, 444);
                        }
                        {
                                btnAdd = new teJButton();
                                getContentPane().add(btnAdd);
                                btnAdd.setText("==>");
                                btnAdd.setBounds(503, 167, 64, 25);
                                btnAdd.setToolTipText("Create the Product/Supplier relationship.");
                                btnAdd.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                                // TODO Auto-generated method stub
                                                if(lstAll.getSelectedIndex()>=0 && lstAssigned.getSelectedIndex()>=0)//makes sure items are selected in both prod and sup boxes
                                                {
                                                        int ProdID, SupID;
                                                        try {
                                                                ResultSet rs = dbCon.getCustomRS("SELECT ProductId FROM Products WHERE ProdName = '" + lstAll.getSelectedValue()+"'");
                                                                rs.next();
                                                                ProdID = rs.getInt(1);
                                                                rs = dbCon.getCustomRS("SELECT SupplierId FROM Suppliers WHERE SupName = '" + lstAssigned.getSelectedValue()+"'");
                                                                rs.next();
                                                                SupID = rs.getInt(1);
                                                                String[] values={dbCon.getGenKey("Products_Suppliers", "ProductSupplierId"), Integer.toString(ProdID), Integer.toString(SupID)};
                                                                if(!dbCon.insert("Products_Suppliers", values))
                                                                {
                                                                        System.out.println("Uh oh, insert failed!");
                                                                }
                                                                rs = dbCon.getCustomRS("select ProdName, SupName, ProductSupplierId, PS.SupplierId, PS.ProductId from Products P, Suppliers S, Products_Suppliers PS where P.ProductId = PS.ProductId and S.SupplierId = PS.SupplierID and S.SupName = '" + lstAssigned.getSelectedValue() +"' and P.ProdName = '" + lstAll.getSelectedValue()+"'");
                                                                ArrayList<ProdSup> psList = new ArrayList<ProdSup>();
                                                                while(rs.next())
                                                                {
                                                                        psList.add(new ProdSup(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
                                                                }
                                                                ListModel PSModel = new DefaultComboBoxModel(psList.toArray(new ProdSup[psList.size()]));
                                                                teJList1.setModel(PSModel);
                                                        } catch (SQLException e1) {
                                                                // TODO Auto-generated catch block
                                                                e1.printStackTrace();
                                                        }
                                                }
                                        }
                                });
                        }
                        {
                                lblAll = new teJLabel();
                                getContentPane().add(lblAll);
                                lblAll.setText("Products:");
                                lblAll.setBounds(12, 90, 93, 14);
                                lblAll.setHorizontalAlignment(SwingConstants.LEFT);
                        }
                        {
                                lblAssigned = new teJLabel();
                                getContentPane().add(lblAssigned);
                                lblAssigned.setText("Suppliers:");
                                lblAssigned.setBounds(249, 90, 139, 14);
                                lblAssigned.setHorizontalAlignment(SwingConstants.LEFT);
                        }
                        {
                                jScrollPane3 = new JScrollPane();
                                getContentPane().add(jScrollPane3);
                                jScrollPane3.setBounds(573, 109, 224, 238);
                                {
                                        ListModel teJList1Model = 
                                                new DefaultComboBoxModel();
                                        teJList1 = new teJList();
                                        jScrollPane3.setViewportView(teJList1);
                                        teJList1.setModel(teJList1Model);
                                        teJList1.setBounds(573, 109, 224, 238);
                                        teJList1.addListSelectionListener(new ListSelectionListener() {
                                                
                                                @Override
                                                public void valueChanged(ListSelectionEvent e) {
                                                        // TODO Auto-generated method stub
                                                        btnSendBack.setEnabled(true);
                                                }
                                        });
                                        
                                }
                        }
                        {
                                teJLabel1 = new teJLabel();
                                getContentPane().add(teJLabel1);
                                teJLabel1.setText("Available Product/Suppliers:");
                                teJLabel1.setHorizontalAlignment(SwingConstants.LEFT);
                                teJLabel1.setBounds(573, 90, 224, 14);
                        }
                        {
                                btnUpdate = new teJButton();
                                getContentPane().add(btnUpdate);
                                btnUpdate.setText("Reset");
                                btnUpdate.setBounds(503, 204, 64, 25);
                                btnUpdate.setToolTipText("Refresh all the listboxes on the form");
                                btnUpdate.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                                // TODO Auto-generated method stub
                                                clearLists();
                                                initLists();
                                        }
                                });
                        }
                        {
                                btnProducts = new teJButton();
                                getContentPane().add(btnProducts);
                                btnProducts.setText("Manage Products");
                                btnProducts.setBounds(12, 359, 219, 25);
                                btnProducts.setToolTipText("Open the Products form to maintain product information.");
                                btnProducts.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                                // TODO Auto-generated method stub
                                                newWindow(new ProductsForm());
/*                                              if(myMDIParent!=null)
                                                        ((MainMDIForm) myMDIParent).launcher(new ProductsForm());
                                                else
                                                {
                                                        ProductsForm prods = new ProductsForm();
                                                        prods.setVisible(true);
                                                }
*/                                      }
                                });
                        }
                        {
                                btnSuppliers = new teJButton();
                                getContentPane().add(btnSuppliers);
                                btnSuppliers.setText("Manage Suppliers");
                                btnSuppliers.setBounds(249, 359, 248, 25);
                                btnSuppliers.setToolTipText("Open the Suppliers form to maintain supplier information.");
                                btnSuppliers.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                                // TODO Auto-generated method stub
                                        	newWindow(new SuppliersForm());
/*                                              if(myMDIParent!=null)
                                                        ((MainMDIForm) myMDIParent).launcher(new SuppliersForm());
                                                else
                                                {
                                                        SuppliersForm sup = new SuppliersForm();
                                                        sup.setVisible(true);
                                                }
*/                                      }
                                });
                        }
                        {
                                btnDelete = new teJButton();
                                getContentPane().add(btnDelete);
                                btnDelete.setText("Delete");
                                btnDelete.setBounds(573, 359, 224, 25);
                                btnDelete.setToolTipText("Delete the Product/Supplier relationship for the product/supplier selected.");
                                btnDelete.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                                // TODO Auto-generated method stub
                                                if(teJList1.getSelectedIndex()>=0)
                                                {
                                                        String pk=      ((ProdSup)teJList1.getSelectedValue()).getProdSupID();
                                                        try {
                                                                dbCon.delete("Products_Suppliers", "ProductSupplierId = "+pk);
                                                        } catch (SQLException e1) {
                                                                // TODO Auto-generated catch block
                                                                e1.printStackTrace();
                                                        }
                                                        clearLists();
                                                        initLists();
                                                }
                                        }
                                });
                        }
                        {
                                lblInstructions = new teJLabel();
                                getContentPane().add(lblInstructions);
                                lblInstructions.setBounds(12, 12, 575, 51);
                                lblInstructions.setText("<html>Select from the products and the suppliers list to narrow down the <br/>Product/Suppliers list. Click the arrow button to add additional Product/Suppliers</html>");
                                lblInstructions.setHorizontalAlignment(SwingConstants.LEFT);
                        }
                        {
                                btnSendBack = new teJButton();
                                getContentPane().add(btnSendBack);
                                btnSendBack.setText("Send Back Selection");
                                btnSendBack.setBounds(574, 59, 223, 25);
                                btnSendBack.setToolTipText("Return the selected Product/Supplier to the packages form.");
                                btnSendBack.addActionListener(new ActionListener() {
                                        
                                        @Override
                                        public void actionPerformed(ActionEvent e) 
                                        {
                                        	if(ParentWindow != null)
                                        	{
                                                // TODO Auto-generated method stub
                                                if(teJList1.getSelectedIndex() != -1)
                                                {
                                                	System.out.println("ProdSup list selected");	 
                                                        toSend = (ProdSup) teJList1.getSelectedValue();
                                                        sendData(toSend);
                                                        setVisible(false);
                                                }
                                            } 
                                        }
                                });
                        }
                        initLists();
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
        private void initLists()
        {
                btnSendBack.setEnabled(false);
                try {
                        ResultSet rs = dbCon.getCustomRS("SELECT ProdName FROM Products");
                        ArrayList<String> pList = new ArrayList<String>();
                        while(rs.next())
                        {
                                pList.add(rs.getString(1));
                        }
                        rs = dbCon.getCustomRS("SELECT SupName FROM Suppliers");
                        ArrayList<String> sList = new ArrayList<String>();
                        while(rs.next())
                        {
                                sList.add(rs.getString(1));
                        }
                        rs = dbCon.getCustomRS("select ProdName, SupName, ProductSupplierId, PS.SupplierId, PS.ProductId from Products P, Suppliers S, Products_Suppliers PS where P.ProductId = PS.ProductId and S.SupplierId = PS.SupplierID");
                        ArrayList<ProdSup> psList = new ArrayList<ProdSup>();
                        while(rs.next())
                        {
                                psList.add(new ProdSup(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
                        }
                        ListModel PModel = new DefaultComboBoxModel(pList.toArray(new String[pList.size()]));
                        lstAll.setModel(PModel);
                        ListModel SModel = new DefaultComboBoxModel(sList.toArray(new String[sList.size()]));
                        lstAssigned.setModel(SModel);
                        ListModel PSModel = new DefaultComboBoxModel(psList.toArray(new ProdSup[psList.size()]));
                        teJList1.setModel(PSModel);
                } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
        }
        private void clearLists()
        {
                lstAll.removeAll();
                lstAssigned.removeAll();
                teJList1.removeAll();
        }
        private void siftList()
        {
                if(lstAssigned.getSelectedIndex()<0 && lstAll.getSelectedIndex()>=0)
                {
                        try {
                                ResultSet rs = dbCon.getCustomRS("select ProdName, SupName, ProductSupplierId, PS.SupplierId, PS.ProductId from Products P, Suppliers S, Products_Suppliers PS where P.ProductId = PS.ProductId and S.SupplierId = PS.SupplierID and P.ProdName = '" + lstAll.getSelectedValue()+"'");
                                ArrayList<ProdSup> psList = new ArrayList<ProdSup>();
                                while(rs.next())
                                {
                                        psList.add(new ProdSup(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
                                }
                                ListModel PSModel = new DefaultComboBoxModel(psList.toArray(new ProdSup[psList.size()]));
                                teJList1.setModel(PSModel);
                        } catch (SQLException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                else if (lstAssigned.getSelectedIndex()>=0 && lstAll.getSelectedIndex()<0)
                {
                        try {
                                ResultSet rs = dbCon.getCustomRS("select ProdName, SupName, ProductSupplierId, PS.SupplierId, PS.ProductId from Products P, Suppliers S, Products_Suppliers PS where P.ProductId = PS.ProductId and S.SupplierId = PS.SupplierID and S.SupName = '" + lstAssigned.getSelectedValue()+"'");
                                ArrayList<ProdSup> psList = new ArrayList<ProdSup>();
                                while(rs.next())
                                {
                                        psList.add(new ProdSup(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
                                }
                                ListModel PSModel = new DefaultComboBoxModel(psList.toArray(new ProdSup[psList.size()]));
                                teJList1.setModel(PSModel);
                        } catch (SQLException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                else
                {
                        try {
                                ResultSet rs = dbCon.getCustomRS("select ProdName, SupName, ProductSupplierId, PS.SupplierId, PS.ProductId from Products P, Suppliers S, Products_Suppliers PS where P.ProductId = PS.ProductId and S.SupplierId = PS.SupplierID and S.SupName = '" + lstAssigned.getSelectedValue() +"' and P.ProdName = '" + lstAll.getSelectedValue()+"'");
                                ArrayList<ProdSup> psList = new ArrayList<ProdSup>();
                                while(rs.next())
                                {
                                        psList.add(new ProdSup(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
                                }
                                ListModel PSModel = new DefaultComboBoxModel(psList.toArray(new ProdSup[psList.size()]));
                                teJList1.setModel(PSModel);
                        } catch (SQLException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
        }
        private void newWindow(teJinternalFrame newWin)
        {
        	MainMDIForm temp = (MainMDIForm)getTopLevelAncestor();
        	temp.addWin(newWin);
        }
        private teJList lstAll;
        private teJButton btnAdd;
        private JScrollPane jScrollPane3;
        private JScrollPane jScrollPane2;
        private JScrollPane jScrollPane1;
        private teJButton btnSendBack;
        private teJLabel lblInstructions;
        private teJButton btnDelete;
        private teJButton btnSuppliers;
        private teJButton btnProducts;
        private teJButton btnUpdate;
        private teJLabel teJLabel1;
        private teJList teJList1;
        private teJLabel lblAssigned;
        private teJLabel lblAll;
        private teJList lstAssigned;

}
